﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour {

    [SerializeField]
    private float speed;
    [SerializeField]
    private float height;
    private bool isJump = true;
	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
        float horizontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");
        this.transform.Translate(new Vector2(horizontal * speed * Time.deltaTime, 0));

        if(isJump)
        {
            if(Input.GetKeyDown(KeyCode.Space))
            {
                this.GetComponent<Rigidbody2D>().AddForce(new Vector2(0, height * 100));
            } 
        }

        if(horizontal < 0)
        {
            GetComponent<SpriteRenderer>().flipX = true;
        }
        else if(horizontal > 0)
        {
            GetComponent<SpriteRenderer>().flipX = false;
        }
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if(collision.gameObject.tag == "Ground")
        {
            isJump = true;
        }
    }

    private void OnCollisionExit2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "Ground")
        {
            isJump = false;
        }
    }
}
